package com.one.efaimaly.order.presenter;

import android.content.Context;

import com.one.common.common.order.AddMaterialItem;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.model.extra.BaseExtra;
import com.one.common.presenter.BaseApiPresenter;
import com.one.common.utils.MyToast;
import com.one.common.utils.StringUtils;
import com.one.common.utils.Toaster;
import com.one.common.view.base.BaseActivity;
import com.one.common.view.base.IView;
import com.one.efaimaly.order.model.OrderModel;
import com.one.efaimaly.order.model.extra.AddMaterialListExtra;

import java.util.ArrayList;
import java.util.List;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/8/5 下午8:26
 * description ： 添加材料
 **/
public class AddMaterialPresenter extends BaseApiPresenter<IView, OrderModel> {
    private AddMaterialListExtra listExtra;

    private List<AddMaterialItem> freeItems = new ArrayList<>();
    private List<AddMaterialItem> payItems = new ArrayList<>();
    private boolean isUpdate = false;

    public AddMaterialPresenter(IView mView, Context mContext) {
        super(mView, mContext, new OrderModel((BaseActivity) mContext));

    }

    @Override
    public void onCreate() {
        super.onCreate();
        listExtra = (AddMaterialListExtra) mActivity.getIntent().getSerializableExtra(BaseExtra.getExtraName());
        if (listExtra != null) {
            // 材料列表
            freeItems = listExtra.getFreeList();
            payItems = listExtra.getPayList();
            isUpdate = listExtra.isUpdate();
        }
    }

    public boolean isUpdate() {
        return isUpdate;
    }

    /**
     * 去确认添加页
     */
    public void confirmAddMaterials() {
        // 验证数量大于0
        for (AddMaterialItem item1 : freeItems) {
            if (item1.getNum() == 0) {
                Toaster.showLongToast("数量必须大于0", MyToast.FAIL);
                return;
            }
        }
        for (AddMaterialItem item2 : payItems) {
            if (item2.getNum() == 0) {
                Toaster.showLongToast("数量必须大于0", MyToast.FAIL);
                return;
            }
        }
        AddMaterialListExtra confirmExtra = new AddMaterialListExtra(freeItems, payItems, listExtra.getOrderId());
        RouterManager.getInstance().go(RouterPath.CONFIRM_MATERIAL, confirmExtra);
        if (mView != null) mView.finishPage();


    }

    public List<AddMaterialItem> getFreeItems() {
        return freeItems;
    }

    public void setFreeItems(List<AddMaterialItem> freeItems) {
        this.freeItems = freeItems;
    }

    public List<AddMaterialItem> getPayItems() {
        return payItems;
    }

    public void setPayItems(List<AddMaterialItem> payItems) {
        this.payItems = payItems;
    }
}
